import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager

# 设置支持中文的字体
font_path = 'C:/Windows/Fonts/simhei.ttf'  # 确保路径正确
font_prop = font_manager.FontProperties(fname=font_path)

# 读取数据
df = pd.read_csv(r"C:\Users\卷\Desktop\程序\Pycharm\data uncovered\douyin_dataset.csv")
print(df.head())

# 删除不需要的列
if 'Unnamed: 0' in df.columns:
    del df['Unnamed: 0']

# 显示基本信息和空值计数
print(df.info())
print(df.isnull().sum())

# 尝试将时间列转换为日期时间格式
df['real_time'] = pd.to_datetime(df['real_time'], errors='coerce')
df['date'] = pd.to_datetime(df['date'], errors='coerce')

# 用户特征处理
user_df = pd.DataFrame()
user_df['uid'] = df['uid'].unique()
user_df.set_index('uid', inplace=True)

user_df['浏览量'] = df.groupby('uid')['like'].count()
user_df['点赞量'] = df.groupby('uid')['like'].sum()
user_df['观看作者数'] = df.groupby('uid').agg({'author_id': pd.Series.nunique})
user_df['观看作品数'] = df.groupby('uid').agg({'item_id': pd.Series.nunique})
user_df['观看作品平均时长'] = df.groupby('uid')['duration_time'].mean()
user_df['观看配乐数'] = df.groupby('uid').agg({'music_id': pd.Series.nunique})
user_df['完整观看数'] = df.groupby('uid')['finish'].sum()
user_df['去过的城市数'] = df.groupby('uid').agg({'user_city': pd.Series.nunique})
user_df['观看作品城市数'] = df.groupby('uid').agg({'item_city': pd.Series.nunique})

print(user_df.describe())
user_df.to_csv('用户特征.csv', encoding='utf_8_sig')

# 作者特征处理
author_df = pd.DataFrame()
author_df['author_id'] = df['author_id'].unique()
author_df.set_index('author_id', inplace=True)

author_df['总浏览量'] = df.groupby('author_id')['like'].count()
author_df['总点赞量'] = df.groupby('author_id')['like'].sum()
author_df['总观完量'] = df.groupby('author_id')['finish'].sum()
author_df['总作品数'] = df.groupby('author_id').agg({'item_id': pd.Series.nunique})

# 计算每个作者的作品平均时长
item_time = df.groupby(['author_id', 'item_id'])['duration_time'].mean().reset_index()
author_df['作品平均时长'] = item_time.groupby('author_id')['duration_time'].mean()

author_df['使用配乐数量'] = df.groupby('author_id').agg({'music_id': pd.Series.nunique})
author_df['发布作品日数'] = df.groupby('author_id')['real_time'].nunique()

# 计算创作活跃度
active_days = df.groupby('author_id')['date'].agg(['min', 'max'])
active_days['创作活跃度(日)'] = (active_days['max'] - active_days['min']).dt.days + 1
author_df = author_df.join(active_days['创作活跃度(日)'])

author_df['去过的城市数'] = df.groupby('author_id').agg({'item_city': pd.Series.nunique})

print(author_df.describe())
author_df.to_csv('作者特征.csv', encoding='utf_8_sig')

# 作品特征处理
item_df = pd.DataFrame()
item_df['item_id'] = df['item_id'].unique()
item_df.set_index('item_id', inplace=True)

item_df['浏览量'] = df.groupby('item_id')['like'].count()
item_df['点赞量'] = df.groupby('item_id')['like'].sum()
item_df['发布城市'] = df.groupby('item_id')['item_city'].first()  # Assuming first occurrence

item_df.to_csv('作品特征.csv', encoding='utf_8_sig')

# 可视化部分
plt.figure(figsize=(15, 8))
plt.bar(user_df.index, user_df['点赞量'])
plt.xlabel('用户ID', fontproperties=font_prop)
plt.ylabel('点赞量', fontproperties=font_prop)
plt.title('用户点赞量分布', fontproperties=font_prop)
plt.xticks(rotation=45, fontproperties=font_prop)
plt.tight_layout()
plt.savefig('用户点赞量分布.png', bbox_inches='tight')  # 确保保存时不裁剪
plt.show()


























